<?php
/**
 * Created by PhpStorm.
 * User: MrHung
 * Date: 5/12/14
 * Time: 9:52 AM
 */

namespace Frontend\Controller;


use Zend\Mvc\Controller\AbstractActionController;
use Frontend\Helper\Sitemap;
use Velacolib\Utility\Utility;
use Zend\View\Model\ViewModel;

class SitemapController extends AbstractActionController
{

    protected $topicModel;

    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        //Utility::CheckLogin($this,'/user/login');

        $this->topicModel = Utility::UserGateway('Admin\Model\AdminTopicTable');


        $this->layout('frontend/layout');

        return parent::onDispatch($e);
    }

    public function indexAction()
    {
        exit('Minimall Sitemap index');
    }

    public function xmlAction()
    {
        $page = $this->topicModel->fetchTopicForSiteMap();
        $file_name = "./public/sitemap.xml";
        $host_url = 'http://minimall.vn/';
        $map = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
        $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;

        foreach ($page as $key => $item) {
            $map .= '<url>';
            $map .= '<loc>' . $host_url . 'raovat/chitiet/' . preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $item['slug']) . '/' . $item['id'] . '</loc>';
            $map .= '</url>' . PHP_EOL;
        }
        for ($i = 0; $i <= 13; $i++) {
            $map .= '<url>';
            $map .= '<loc>' . $host_url . '?search_loc=' . $i . '</loc>';
            $map .= '</url>' . PHP_EOL;
        }
        $map .= '</urlset>';


//        header ("content-type: text/xml");


        file_put_contents($file_name, $map);

//submitting site map to Google, Yahoo, Bing, Ask and Moreover services
//        $utility = new Utility();
//
//        $utility::ping('http://minimall.vn/sitemap.xml');
        exit('Sitemap have been updated!!');

        // Only render the sitemap helper, without any layout
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
} 